#include "ShopScene.h"
#include "LobbyScene.h"

using namespace cocos2d;
using namespace cocos2d::extension;

CCScene* ShopScene::scene()
{
    CCScene * scene = NULL;
    do
    {
        scene = CCScene::create();
        CC_BREAK_IF(! scene);
        ShopScene *layer = ShopScene::create();
        CC_BREAK_IF(! layer);
        scene->addChild(layer);
    } while (0);
    
    return scene;
}

bool ShopScene::init()
{
    if ( !CCLayer::init() )
	{
		return false;
    }
    
    this->setTouchEnabled(true);
    this->setTouchMode(kCCTouchesAllAtOnce);
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                                          "btn_nor.png",
                                                          "btn_focus.png",
                                                          this,
                                                          menu_selector(ShopScene::enterLobbyCallback));
    
	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                origin.y + pCloseItem->getContentSize().height/2));
    
    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition(CCPointZero);
    this->addChild(pMenu, 1);
    
    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");
    
    // position the sprite on the center of the screen
    pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    
    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    CCTableView* tableView = CCTableView::create(this, CCSizeMake(320, 200));
    tableView->setDirection(kCCScrollViewDirectionVertical);
    tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
    tableView->setPosition(ccp(300, 300));
    tableView->setDelegate(this);
    
    this->addChild(tableView);
    
    return true;
}

void ShopScene::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
    
}

CCTableViewCell* ShopScene::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
    CCTableViewCell* cell = table->dequeueCell();
    
    if(!cell)
    {
        cell = new CCTableViewCell();
        cell->autorelease();
        
        CCLabelTTF* pLabel = CCLabelTTF::create("우리동네 체대형!!", "Arial", 50);
        cell->addChild(pLabel);
    }

    return cell;
}

unsigned int ShopScene::numberOfCellsInTableView(CCTableView *table)
{
    return 50;
}

void ShopScene::enterLobbyCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->replaceScene(CCTransitionSplitCols::create(1.0, LobbyScene::scene()));
}
